package com.deli.pagingsimple;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.deli.pagingforjava.adapter.BasePagingAdapter;
import com.deli.pagingforjava.bean.BaseItemBean;
import com.deli.pagingsimple.bean.CategoryBean;
import com.deli.pagingsimple.bean.HeaderBean;
import com.deli.pagingsimple.bean.ItemBean;


/**
 * @Author: QDL
 * @CreateDate: 2021/4/23 14:22
 */
public class MainPagingAdapter extends BasePagingAdapter<BaseItemBean, RecyclerView.ViewHolder> {
    Context context;
    LayoutInflater layoutInflater;

    public MainPagingAdapter(Context context) {
        super(context, diffCallback);
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onPagingCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HeaderViewHolder.ITEM_TYPE) {
            return new HeaderViewHolder(layoutInflater.inflate(R.layout.header_view, parent, false));
        } else if (viewType == CategoryViewHolder.ITEM_TYPE) {
            return new CategoryViewHolder(layoutInflater.inflate(R.layout.category_item, parent, false));
        }
        return new ItemViewHolder(layoutInflater.inflate(R.layout.item_view, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ((ItemViewHolder) holder).label.setText(((ItemBean) getItem(position)).getName());
        }
    }

    @Override
    public int getPagingItemViewType(int position) {
        if (getItem(position) instanceof HeaderBean) {
            return HeaderViewHolder.ITEM_TYPE;
        } else if (getItem(position) instanceof CategoryBean) {
            return CategoryViewHolder.ITEM_TYPE;
        }

        return ItemViewHolder.ITEM_TYPE;
    }

    public static class ItemViewHolder extends RecyclerView.ViewHolder {
        TextView label;
        public final static int ITEM_TYPE = 0;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            label = itemView.findViewById(R.id.label);
        }
    }

    static DiffUtil.ItemCallback<BaseItemBean> diffCallback = new DiffUtil.ItemCallback<BaseItemBean>() {
        @Override
        public boolean areItemsTheSame(@NonNull BaseItemBean oldItem, @NonNull BaseItemBean newItem) {
            return oldItem.getClass() == newItem.getClass()
                    && oldItem == newItem;
        }

        @Override
        public boolean areContentsTheSame(@NonNull BaseItemBean oldItem, @NonNull BaseItemBean newItem) {
            return false;
        }
    };


    protected static class HeaderViewHolder extends RecyclerView.ViewHolder {
        public final static int ITEM_TYPE = 1;

        public HeaderViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }


    protected static class CategoryViewHolder extends RecyclerView.ViewHolder {
        public final static int ITEM_TYPE = 2;

        public CategoryViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
